package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.SmsCouponParam;
import com.macro.mall.model.SmsCoupon;
import com.macro.mall.service.SmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 优惠券管理Controller
 * @author Administrator
 * @date 2020/5/30
 * @since
 */
@Controller
@Api(tags = "SmsCouponController", description = "优惠券管理")
@RequestMapping("/coupon")
public class SmsCouponController {
	@Autowired
	private SmsCouponService smsCouponService;

	@ApiOperation("添加优惠券")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult add(@RequestBody SmsCouponParam couponParam) {
		int count = smsCouponService.create(couponParam);
		if(count>0){
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("删除优惠券")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult delete(@PathVariable
		Long id) {
		int count = smsCouponService.delete(id);
		if(count>0){
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("修改优惠券")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult update(@PathVariable Long id,@RequestBody SmsCouponParam couponParam) {
		int count = smsCouponService.update(id,couponParam);
		if(count>0){
			return CommonResult.success(count);
		}
		return CommonResult.failed();
	}

	@ApiOperation("根据优惠券名称和类型分页获取优惠券列表")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult<CommonPage<SmsCoupon>> list(
		@RequestParam(value = "name",required = false) String name,
		@RequestParam(value = "type",required = false) Integer type,
		@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
		@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		List<SmsCoupon> couponList = smsCouponService.list(name,type,pageSize,pageNum);
		return CommonResult.success(CommonPage.restPage(couponList));
	}

	@ApiOperation("获取单个优惠券的详细信息")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult<SmsCouponParam> getItem(@PathVariable Long id) {
		SmsCouponParam couponParam = smsCouponService.getItem(id);
		return CommonResult.success(couponParam);
	}
}
